﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraTab;
using DevExpress.XtraTab.ViewInfo;
using QLCONGNOAPPs.UserForms.Logins;
using QLCONGNOAPPs.UserForms.FlashScreens;
using QLCONGNOLIBS.Securities;
using QLCONGNOLIBS.Controllers;
using QLCONGNOAPPs.UserControls;
using QLCONGNOAPPs.UserControls.Products;
using QLCONGNOAPPs.UserControls.Zones;
using QLCONGNOAPPs.UserControls.Citys;
using QLCONGNOAPPs.UserControls.Departments;
//using QLCONGNOLIBS.Securities.DungDD_SecurityFramework;
//using QLCONGNOLIBS.Controllers.DungDD_Controller;


namespace QLCONGNOAPPs
{
    public partial class MAINAPPS : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private SqlConnection m_objConnection;
        private Commons.Services m_objServices;
        private ucPanelControls m_objPanel;
        //new        
        private static SecurityEntity m_objSecurity;
        private UsersControllers m_objUserController;        
        //
        private XtraTabPage xtraTabPage_StartPage;
        private XtraTabPage xtraTabPage_KhuVuc;
        private XtraTabPage xtraTabPage_PhongBan;
        //
        public MAINAPPS()
        {
            InitializeComponent();

            //string a = Guid.NewGuid().ToString();
            m_objServices = new Commons.Services();
            m_objConnection = new SqlConnection();
            m_objConnection = m_objServices.getConnection();

            m_objSecurity = new SecurityEntity();
            m_objUserController = new UsersControllers(m_objConnection, m_objSecurity); 

            //
            xtraTabPage_StartPage = new XtraTabPage();
            xtraTabPage_KhuVuc = new XtraTabPage();
            xtraTabPage_PhongBan = new XtraTabPage();
            //
        }

        private void MAINAPPS_Load(object sender, EventArgs e)
        {
            //Hiển thị form giới thiệu phần mềm
            frmFlash frm = new frmFlash();
            frm.ShowDialog();

            //Thực thi nghiệp vụ kiểm tra thông tin đăng nhập
            Login();
        }

        #region Login()
        void Login()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                //Khai bao khoi tao doi tuong login Forms
                if (m_objConnection.State == ConnectionState.Closed)
                {
                    m_objConnection = m_objServices.getConnection();
                }
                m_objSecurity = new SecurityEntity();
                frmLoginInfo frm = new frmLoginInfo(m_objConnection, m_objSecurity);
                frm.ShowDialog();

                if (!frm.bClose)
                    //Kiem tra thong tin nguoi dung
                    CheckLoginSystem(m_objSecurity);
                else
                    CheckLoginSystem(m_objSecurity);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi xảy ra. Chi tiết: " + ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        #endregion
        #region void CheckLoginSystem(SecurityEntity objContext)
        void CheckLoginSystem(SecurityEntity objContext)
        {
            if (objContext.LoginSuccess)
            {
                //Hiển chức năng
                this.Text = String.Format("Xin chào: {0} đang sử dụng PHẦN MỀM QUẢN LÝ CÔNG NỢ!",
                    objContext.FullName);
                LoadXtraStartPage();
            }
            else
            {
                this.Text = "Xin chào Guest!";
                DisableRibbon();
            }
        }
        #endregion

        /// <summary>
        /// Disable ribbon form if login faild
        /// </summary>
        public void DisableRibbon()
        {
            foreach (var item in ribbon.Items)
            {
                if (item is BarItem)
                {
                    ((BarItem)item).Enabled = false;
                }
            }
        }
        /// <summary>
        /// Load xtra start page
        /// </summary>
        public void LoadXtraStartPage()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (xtraTabControl_Main.TabPages.Contains(xtraTabPage_StartPage))
                {
                    xtraTabControl_Main.TabPages.Remove(xtraTabPage_StartPage);                                                    
                }
                    xtraTabPage_StartPage = new XtraTabPage();
                    xtraTabControl_Main.TabPages.Add(xtraTabPage_StartPage);                    
                    xtraTabPage_StartPage.Text = "Trang khởi động";
                    xtraTabControl_Main.Images = imageList1;
                    xtraTabPage_StartPage.ImageIndex = 0;
                    xtraTabControl_Main.SelectedTabPage = xtraTabPage_StartPage;
                    m_objPanel = new ucPanelControls(m_objConnection, m_objSecurity);
                    xtraTabPage_StartPage.Controls.Add(m_objPanel);                                 
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi xảy ra. Chi tiết: " + ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void xtraTabControl_Main_CloseButtonClick(object sender, EventArgs e)
        {
            ClosePageButtonEventArgs arg = e as ClosePageButtonEventArgs;
            if (arg != null)
            {
                xtraTabControl_Main.TabPages.Remove(arg.Page as XtraTabPage);
                if (arg.Page.Text.Equals("Trang khởi động"))
                {                    
                    xtraTabPage_StartPage = new XtraTabPage();
                    xtraTabControl_Main.TabPages.Add(xtraTabPage_StartPage);
                    xtraTabPage_StartPage.Text = "Trang khởi động";
                    xtraTabControl_Main.Images = imageList1;
                    xtraTabPage_StartPage.ImageIndex = 0;
                    xtraTabControl_Main.SelectedTabPage = xtraTabPage_StartPage;
                    m_objPanel = new ucPanelControls(m_objConnection, m_objSecurity);
                    xtraTabPage_StartPage.Controls.Add(m_objPanel);
                }
            }                
        }

        private void barButton_TrangKhoiDong_ItemClick(object sender, ItemClickEventArgs e)
        {
            LoadXtraStartPage();
        }
        /// <summary>
        /// barButton_KhuVuc_ItemClick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButton_KhuVuc_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (xtraTabControl_Main.TabPages.Contains(xtraTabPage_KhuVuc))
                {
                    xtraTabControl_Main.SelectedTabPage = xtraTabPage_KhuVuc;
                }
                else
                {
                    xtraTabControl_Main.TabPages.Add(xtraTabPage_KhuVuc);
                    xtraTabPage_KhuVuc.Text = "Khu vực";
                    xtraTabControl_Main.SelectedTabPage = xtraTabPage_KhuVuc;
                    ucCitys objCity = new ucCitys();
                    xtraTabPage_KhuVuc.Controls.Add(objCity);
                    objCity.Dock = DockStyle.Fill;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi xảy ra. Chi tiết: " + ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
           
        }

        private void barButton_PhongBan_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (xtraTabControl_Main.TabPages.Contains(xtraTabPage_PhongBan))
                {
                    xtraTabControl_Main.SelectedTabPage = xtraTabPage_PhongBan;
                }
                else
                {
                    xtraTabControl_Main.TabPages.Add(xtraTabPage_PhongBan);
                    xtraTabPage_PhongBan.Text = "Phòng ban";
                    xtraTabControl_Main.SelectedTabPage = xtraTabPage_PhongBan;
                    ucDepartments objDepart = new ucDepartments();
                    xtraTabPage_PhongBan.Controls.Add(objDepart);
                    objDepart.Dock = DockStyle.Fill;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi xảy ra. Chi tiết: " + ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

    }
}